package logs

import (
	"Gin-Server-01/com/server/core/config"
	"Gin-Server-01/com/server/core/tools/times"
	"github.com/fatih/color"
	"io"
	"log"
	"os"
	"path/filepath"
)

/**
 * @Author Administrator
 * @Description
 * @Date 2023/12/3 18:04
 * @Version 1.0
 */
//  控制台颜色

var (
	Red     = color.New(color.FgRed).PrintfFunc()
	Green   = color.New(color.FgGreen).PrintfFunc()
	Yellow  = color.New(color.FgYellow).PrintfFunc()
	Blue    = color.New(color.FgBlue).PrintfFunc()
	Magenta = color.New(color.FgMagenta).PrintfFunc()
	Cyan    = color.New(color.FgCyan).PrintfFunc()
	White   = color.New(color.FgWhite).PrintfFunc()
)

//
//  Setting
//  @Description: 设置
//

type Setting struct {
	Color func(format string, a ...interface{})
}

// 父文件夹
var cfg = config.Logs{}

func init() {
	if cfg.Status == true {
		//cfg = common.Configs.Logs
		//cfg = common.Configs.Logs
		if len(cfg.SavePath) == 0 {
			cfg.SavePath = "log/"
		}

		if len(cfg.BasePath) == 0 {
			cfg.BasePath = "run.log"
		}

		file, _ := os.Create(cfg.SavePath + cfg.BasePath + "" + times.FormatDateTime())
		log.SetOutput(file)
	}
}

// PrintLogByFilePath
//
//	@Description: 按文件路径打印日志
//	@param: filePath 文件路径
//	@param: prefix 前缀
//	@param: messages 消息
func PrintLogByFilePath(filePath string, prefix string, setting *Setting, messages ...string) {
	// 获取日志文件的绝对路径
	absPath, err := filepath.Abs(filePath)
	if err != nil {
		log.Printf("获取绝对路径时出错：%s\n", err)
		return
	}

	// 打开或创建日志文件
	file, err := os.OpenFile(absPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Printf("无法打开或创建日志文件：%s\n", err)
		return
	}
	defer file.Close()

	// 创建一个新的Logger实例，将输出重定向到文件和控制台
	if cfg.Status == true {
		multiWriter := io.MultiWriter(file, os.Stdout)
		logger := log.New(multiWriter, prefix, log.Ldate|log.Ltime|log.Lshortfile)
		log.SetOutput(multiWriter)
		// 记录字符串参数到文件和控制台中
		for _, message := range messages {
			logger.Println(message)
		}
	}

}

// PrintlnByColorAndTag
//
//	@Summary:
//	@Description: 通过自定义颜色和tag打印
//	@Param: setColor
//	@Param: tag
//	@Param: messages
func PrintlnByColorAndTag(setColor *Setting, tag string, messages ...string) {
	PrintLogByFilePath(cfg.BasePath, "【"+tag+"】", setColor, messages...)
}

// Println
//
//	@Description: 打印
//	@param: prefix 前缀
//	@param: messages 消息
func Println(messages ...string) {
	date := times.FormatDate()
	PrintLogByFilePath(cfg.SavePath+date+".log", "[Application]"+"\t", nil, messages...)
}

// ControllerPrintln
//
//	@Description:
//	@param: messages
func ControllerPrintln(messages ...string) {
	date := times.FormatDate()
	PrintLogByFilePath(cfg.SavePath+date+".log", "【Controller】"+"\t", nil, messages...)
}

// ServicePrintln
//
//	@Description:
//	@param: messages
func ServicePrintln(messages ...string) {
	date := times.FormatDate()
	PrintLogByFilePath(cfg.SavePath+date+".log", "【Service】"+"\t", nil, messages...)
}

// DaoPrintln
//
//	@Description:
//	@param: messages
func DaoPrintln(messages ...string) {
	date := times.FormatDate()
	PrintLogByFilePath(cfg.SavePath+date+".log", "【DAO】"+"\t", nil, messages...)
}
